<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	#import {
		position: relative;
		top: 1.4px;
	}
</style>
<div class="page-header">
	<h1>
		课室管理
		<!-- 非同步专业才支持导入 -->
		<?php if(! $rmxSchool['school_from']): ?>
			<a href="#modal-classroom" class="btn btn-info a-custom" data-toggle="modal">
				<i class="ace-icon fa fa-plus"></i> 新增课室
			</a>
			<span class="dropdown dropdown-custom">
				<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
					<i class="ace-icon fa fa-database"></i> 模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
				</div>
				<ul class="dropdown-menu">
					<li>
						<a href="javascript:" id="import">
							<i class="ace-icon fa fa-upload"></i> 导入课室数据
						</a>
					</li>
					<li>
						<a href="https://resource.rongmixiao.com/2019/12/16/9c656e03fb9071a207d045dfaaed1661.xls" target="_blank">
							<i class="ace-icon fa fa-download"></i> 导出课室数据模板
						</a>
					</li>
				</ul>
			</span>
			<?php if($existClassroomData): ?>
				<a href="<?= admin_url('classes/export_classroom_data') ?>" class="btn btn-info a-custom" target="_blank">
					<i class="ace-icon fa fa-download"></i> 导出课室数据
				</a>
			<?php endif ?>
			<a href="javascript:;" class="btn btn-danger a-custom a-delete-batch">
				<i class="ace-icon fa fa-times"></i> 批量删除课室
			</a>
		<?php endif ?>
	</h1>
</div>

<div class="col-xs-12">
	<!-- 筛选 -->
	<div class="widget-box">
		<div class="widget-header">
			<h4 class="widget-title">内容筛选</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main" style="padding-top:15px;">
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">课室名称</label>
					<div class="keyword-search-box">
						<div class="div-search">
							<input style="float:left;height:30px;width:180px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px 0 0 10px !important;-moz-border-radius:10px 0 0 10px;" type="text" name="keyword" placeholder="搜索课室名称..." autocomplete="off">
							<span id="empty-search" style="position:relative;right:110px;cursor:pointer;display:none;">×</span>
							<button id="btn-search" class="btn-search btn-info" style="outline:none;float:left;height:30px;width:44px;border:1px solid #669FC7;border:0;border-radius:0 10px 10px 0;-moz-border-radius:0 10px 10px 0;margin-right:40px;">
								<i class="ace-icon fa fa-search nav-search-icon white"></i>
							</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<!-- 非同步专业才支持导入 -->
				<?php if(! $rmxSchool['school_from']): ?>
					<th class="center">
						<label class="check-th">
							<input type="checkbox" class="ace" id="check-first">
							<span class="lbl"></span>
						</label>
					</th>
				<?php endif ?>	
				<th>课室名称</th>
				<th>班级名称</th>
				<th>创建时间</th>
				<?php if(empty($rmxSchool['school_from'])): ?>
					<th>操作</th>
				<?php endif ?>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 新增账号弹窗 -->
<div id="modal-classroom" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					课室管理
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="classroom-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">所属校区：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" disabled="" value="<?=$campusInfo['campus_name']?>" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">课室名称：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="room_name" placeholder="请填写课室名称"
										
									/>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-classroom">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){

		// 保存课室按钮
		var classroomForm = $("#classroom-form");
		$("#save-classroom").click(function(){
			classroomForm.submit();
		})
		var submitUrl = "<?=admin_url('classes/save_classroom_action')?>";
		form_submit(classroomForm, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})
		// 编辑课室
		$("body").on("click", ".edit-classroom", function(){
			classroomForm.find("[name=id]").val($(this).data("id"))
			classroomForm.find("[name=room_name]").val($(this).data("name"))

			// 如果是同步的数据，设置不可编辑
			if($(this).data("xid")){
				classroomForm.find("[name=room_name]").attr("disabled","disabled")
			} else{
				classroomForm.find("[name=room_name]").removeAttr("disabled")
			}
		})

		// 课室表格
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('classes/get_classroom_list')?>',
			"columns": [
				// 非同步专业才支持导入
				<?php if(! $rmxSchool['school_from']): ?>
					{"data": function(ret){
						return "<div class='center'><label class='check-td'><input type='checkbox' class='ace check-list' value=" + ret.id + "><span class='lbl'></span></label></div>";
					}},
				<?php endif ?>
	            {"data": "classroom_name"},
	            {"data": "class_name"},
	            {"data": "createtime"},
				<?php if(empty($rmxSchool['school_from'])): ?>
					{"data": function(ret){
						var action = '';
							action += '<a class="edit-classroom green mr20" href="#modal-classroom" data-toggle="modal" data-id="'+ ret.id +'" data-name="'+ ret.classroom_name +'" data-xid="'+ ret.x_classroom_id +'"><i class="fa fa-edit"></i> 修改</a>';
							action += '<a class="text-danger delete-classroom" href="javascript:" data-id=' + ret.id +  ' data-title=' + ret.classroom_name + '><i class="fa fa-times"></i> 删除</a>';
						return action;
					}},
				<?php endif ?>
			]
		});
		// 删除课室
		$("body").on('click', '.delete-classroom', function() {
			var id = $(this).data('id');
			var title = $(this).data('title');
			var posturl = "<?= admin_url("classes/delete_classroom_action") ?>";
			var data = {
				'id': id
			};
			layer.confirm('确定删除：' + title + '？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.reload();
				});
			});
		});
		// 导入
		var upFromurl = '<?=UP_FORM_URL?>';
		var signature = '<?=$signature?>';
		var policy = '<?=$policy?>';
		$("#import").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				ajax_post("<?= admin_url('import/import_classroom') ?>", { fileLink: fileLink}, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 设置选择文件按钮大小
		setTimeout(function(){
			$(".webuploader-pick").next().width(158).height(32);
		}, 100);

		// 全选复选款
		$("#check-first").change(function(e) { 
			var checked =  $("#check-first").is(':checked'); 
			if(checked){
				$('.check-list').each(function (i){
					$(this).prop('checked', true);
				})
			} else{
				$('.check-list').each(function (i){
					$(this).prop('checked', false);
				})
			}
		});
		setTimeout(() => {
			$(".check-list").change(function(e) { 
				var checked = $(this).is(':checked');
				if(checked) {
					var allChecked = true;
					$('.check-list').each(function (i){
						if(!$(this).is(':checked')) {
							allChecked = false;
						}
					})
					if(allChecked) {
						$("#check-first").prop('checked', true);
					} else {
						$("#check-first").prop('checked', false);
					}
				} else {
					$("#check-first").prop('checked', false);
				}
			});
		}, 500);
		setTimeout(() => {
			$(".check-list").change(function(e) { 
				var checked = $(this).is(':checked');
				if(checked) {
					var allChecked = true;
					$('.check-list').each(function (i){
						if(!$(this).is(':checked')) {
							allChecked = false;
						}
					})
					if(allChecked) {
						$("#check-first").prop('checked', true);
					} else {
						$("#check-first").prop('checked', false);
					}
				} else {
					$("#check-first").prop('checked', false);
				}
			});
		}, 2000);

		// 批量删除
		$(".a-delete-batch").click(function(){
			var list = $("input:checkbox.check-list:checked");
			var idArr = new Array();
			list.each(function(){
				idArr.push($(this).val());
			});
			var len = idArr.length;
			var postUrl = "<?=admin_url('school/delete_classroom_batch')?>";
			var postData = {"id_arr": idArr};
			if(len == 0) {
				ajax_post(postUrl, postData, function(res){
					window.location.reload();
				});
				return;
			}
			layer.confirm('确认批量删除课室？删除个数：' + len, { title:"提示", offset: '160px' }, function(){
				ajax_post(postUrl, postData, function(res){
					window.location.reload();
				})
			});
		});

		// 内容筛选
		$("#data-search-btn").click(function(){
			table.setAjaxData({
				"keyword": $("[name=keyword]").val(),
			});
		});
		// 敲回车就搜索
		$("[name=keyword]").keypress(function (e) {
                if (e.which == 13) {
                    $("#btn-search").trigger("click");
                }
		});
		// 清空搜索框
		$("[name=keyword]").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=keyword]").val('');
					$(this).hide();
					// 重置搜索结果
					table.setAjaxData({
					});
				})
			} else {
				$("#empty-search").hide();
			}
		}); 
		// 搜索标题
		$("#btn-search").click(function() {
			var keyword = $("[name=keyword]").val();
			if(keyword != '') {
				table.setAjaxData({
					"keyword": keyword
				})
			}
		})
	})
</script>